<template>
  <div style="height:100%;width:100%;position:relative;" ref="maindev">
      <div class="pagetop" ref="buttondiv">
          <table style="font-size:12px;border-spacing: 5px;" border="0">
              <tr>
                  <td align="right" style="width:60px">标本日期</td>
                  <td>
                      <el-date-picker v-model="datetime1" type="date" placeholder="开始日期" value-format="yyyy-MM-dd" size="mini" :clearable="false" style="width:130px"></el-date-picker> -
                      <el-date-picker v-model="datetime2" type="date" placeholder="开始日期" value-format="yyyy-MM-dd" size="mini" :clearable="false" style="width:130px"></el-date-picker>
                  </td>
                  <td align="right" style="width:60px">申请科室</td>
                  <td>
                      <el-select v-model="reqdeptno" @dblclick.native="popMultipleChk(reqdeptno, 'reqdeptno', deptOption, '科室选择')" filterable clearable style="width:100%">
                          <el-option v-for="(item, index) in deptOption" :key="index" :value="item.data" :label="item.display">
                          </el-option>
                      </el-select>
                  </td>
                  <td>统计项目</td>
              </tr>
              <tr>
                  <td align="right" style="width:60px">报告单元</td>
                  <td>
                      <el-select v-model="rptunitid" @dblclick.native="popMultipleChk(rptunitid, 'rptunitid', rptunitOption, '报告单元选择')" filterable clearable style="width:100%">
                          <el-option v-for="(item, index) in rptunitOption" :key="index" :value="item.data" :label="item.display">
                          </el-option>
                      </el-select>
                  </td>
                  <td align="right" style="width:60px">病人病区</td>
                  <td>
                      <el-select v-model="wardno" @dblclick.native="popMultipleChk(wardno, 'wardno', wardOption, '病区选择')" filterable clearable style="width:100%">
                          <el-option v-for="(item, index) in wardOption" :key="index" :value="item.data" :label="item.display">
                          </el-option>
                      </el-select>
                  </td>
                  <td align="left">
                      <el-checkbox v-model="reportuserflag" true-label="1" false-label="0" @change="clearData($event, 'reportuserflag')">检验医生</el-checkbox>
                  </td>
                  <td align="left">
                      <el-checkbox v-model="deptnoflag" true-label="1" false-label="0" @change="clearData($event, 'deptnoflag')">申请科室</el-checkbox>
                  </td>
                  <td align="left">
                      <el-checkbox v-model="sexflag" true-label="1" false-label="0" @change="clearData($event, 'sexflag')">病人性别</el-checkbox>
                  </td>
                  <td align="left">
                      <el-checkbox v-model="yearflag" true-label="1" false-label="0" @change="clearData($event, 'yearflag')">年</el-checkbox>
                  </td>

              </tr>
              <tr>
                  <td align="right">检验医生</td>
                  <td>
                      <rm-ddl-common datatype="userinfo" dataname="检验医生" v-model="reportuser" width="100%" @change="reportuserChange"></rm-ddl-common>
                  </td>
                  <td align="right">检验组别</td>
                  <td>
                      <rm-ddl-common datatype="labgroup" dataname="检验工作组" v-model="labgroupid" width="100%" @change="labgroupChange"></rm-ddl-common>
                  </td>
                  <td align="left">
                      <el-checkbox v-model="chkuserflag" true-label="1" false-label="0" @change="clearData($event, 'chkuserflag')">审核医生</el-checkbox>
                  </td>
                  <td align="left">
                      <el-checkbox v-model="wardflag" true-label="1" false-label="0" @change="clearData($event, 'wardflag')">病人病区</el-checkbox>
                  </td>

                  <td align="left">
                      <el-checkbox v-model="pattypeflag" true-label="1" false-label="0" @change="clearData($event, 'pattypeflag')">病人类型</el-checkbox>
                  </td>

                  <td align="left">
                      <el-checkbox v-model="monthflag" true-label="1" false-label="0" @change="clearData($event, 'monthflag')">月</el-checkbox>
                  </td>
              </tr>
              <tr>
                  <td align="right" style="width:60px">病人类型</td>
                  <td>
                      <el-select v-model="pattype" @dblclick.native="popMultipleChk(pattype, 'pattype', pattypeOption, '病人类型选择')" filterable clearable style="width:100%">
                          <el-option v-for="(item, index) in pattypeOption" :key="index" :value="item.data" :label="item.display">
                          </el-option>
                      </el-select>
                  </td>
                  <td></td>
                  <td>
                      <el-checkbox v-model="onlyautochk" true-label="1" false-label="0">仅统计参与自动审核的报告</el-checkbox>
                  </td>

                  <td align="left">
                      <el-checkbox v-model="chktipsflag" true-label="1" false-label="0" @change="clearData($event, 'chktipsflag')">审核信息</el-checkbox>
                  </td>
                  <td align="left">
                      <el-checkbox v-model="rptunitflag" true-label="1" false-label="0" @change="clearData($event, 'rptunitflag')">报告单元</el-checkbox>
                  </td>
                  <td align="left">
                      <el-checkbox v-model="labgroupflag" true-label="1" false-label="0" @change="clearData($event, 'labgroupflag')">检验组别</el-checkbox>
                  </td>
                  <td align="left">
                      <el-checkbox v-model="dayflag" true-label="1" false-label="0" @change="clearData($event, 'dayflag')">日</el-checkbox>
                  </td>
              </tr>

          </table>
          <div style="width:100%;text-align:right;margin-right:5px">
              <el-button @click.native="QueryData()" v-has="'PINQUERY'" type="primary" icon="el-icon-search">查询</el-button>
              <el-button @click.native="BtnClear" icon="el-icon-close">清除</el-button>
              <el-button @click.native="BtnExport" icon="el-icon-download">导出</el-button>
              <!-- <el-button @click.native="BtnPrint(1)" icon="el-icon-printer">打印</el-button>
              <el-button @click.native="BtnPrint(3)" class="el-icon-printer">打印样式设计</el-button> -->
          </div>
      </div>
      <div class="pagecenter" ref="centerdev">
          <vxe-table show-overflow ref="xTable1" height="auto" :loading="loading" :data.sync="tableData" class="editable-footer" :footer-method="footerMethod" :footer-cell-class-name="footerCellClassName" border size="mini" show-header-overflow highlight-current-row highlight-hover-row show-footer resizable stripe>
              <vxe-table-column :field="item.field" :title="item.title" :key="item.field" width="80" header-align="center" v-for="item in tableColumns"></vxe-table-column>
              <vxe-table-column field="cp_cnt" title="项次" width="120" header-align="center" align="right"></vxe-table-column>
              <vxe-table-column field="chkpass" title="审核通过" width="120" header-align="center" align="right"></vxe-table-column>
              <vxe-table-column field="chknopass" title="审核未通过" width="120" header-align="center" align="right"></vxe-table-column>
              <vxe-table-column field="elsenum" title="其他" width="120" header-align="center" align="right"></vxe-table-column>
              <vxe-table-column field="passrate" title="通过率%" width="120" header-align="center" align="right"></vxe-table-column>

          </vxe-table>
      </div>
      <!--多选弹窗--->
      <el-dialog width="900px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" :title="multichktitle" :visible.sync="dialogMultipleVisible" :destroy-on-close="true" v-if="dialogMultipleVisible">
          <rm-SqlMultiple :leftOption="option1" :rightOption="option2" :name="fieldname" @change="closedialog"></rm-SqlMultiple>
      </el-dialog>
  </div>
</template>

<script type="text/javascript">
import XEUtils from "xe-utils";
export default {
  name: "LIS634",
  data() {
      return {
          buttonwidth: "",
          //筛选项目
          datetime1: "",
          datetime2: "",
          //统计项目
          reportuserflag: "0",
          deptnoflag: "0",
          sexflag: "0",
          chkuserflag: "0",
          wardflag: "0",
          yearflag: "0",
          pattypeflag: "0",
          labgroupflag: "0",
          monthflag: "0",
          chktipsflag: "0",
          rptunitflag: "0",
          dayflag: "0",
          selectarr: [],
          //查询条件
          wardno: "", //病区
          reqdeptno: "", //科室
          labgroupid: "", //检验组别
          rptunitid: "", //报告单元
          reportuser: "", //检验医生
          onlyautochk: "1", //仅统计参与自动审核的报告
          pattype: "",
          //表格属性
          loading: false,
          //表格内容
          tableData: [],
          tableColumns: [],
          //多选弹窗
          rptunitOption: [],
          pattypeOption: [],
          deptOption: [],
          labgroupOption: [],
          wardOption: [],
          multichktitle: "",
          dialogMultipleVisible: false,
          option1: [],
          option2: [],
          fieldname: "",
          //以下为排序数组
          rptStyleSettingState: false,
          reportusername: "",
          labgroupname: "",
      };
  },
  created() {
      this.QueryOption();
  },
  updated() {},
  methods: {
      QueryData() {
          var selectstr = "";
          if (this.selectarr.length > 0) {
              for (let i = 0; i < this.selectarr.length; i++) {
                  selectstr += this.selectarr[i] + ",";
              }
              selectstr = selectstr.substring(0, selectstr.length - 1);
          }
          if (selectstr == "") {
              this.$alert("请选择统计项目!", "提示");
              return;
          }
          this.loading = true;
          this.$tool
              .request({
                  url: "LIS/LIS6/LIS634",
                  method: "post",
                  data: {
                      action: "queryData",
                      sampledate1: this.datetime1,
                      sampledate2: this.datetime2,
                      rptunitid: this.rptunitid,
                      reportuser: this.reportuser,
                      pattype: this.pattype,
                      reqdeptno: this.reqdeptno,
                      wardno: this.wardno,
                      labgroupid: this.labgroupid,
                      onlyautochk: this.onlyautochk,
                      selectstr: selectstr
                  }
              })
              .then(res => {
                  this.loading = false;
                  this.tableData = res.RData.Data;
                  for (let i = 0; i < this.tableData.length; i++) {
                      if (this.tableData[i].chkpass > 0 || this.tableData[i].chknopass > 0) {
                          this.tableData[i].passrate = (this.tableData[i].chkpass / (this.tableData[i].chkpass + this.tableData[i].chknopass)) * 100;
                          this.tableData[i].passrate = this.tableData[i].passrate.toFixed(2);
                      }
                  }
                  this.tableColumns = res.RData.Cols;
              })
              .catch(err => {
                  this.loading = false;
              });
      },
      QueryOption() {
          this.$tool
              .request({
                  url: "LIS/LIS6/LIS634",
                  method: "post",
                  data: {
                      action: "initOption"
                  }
              })
              .then(res => {
                  this.deptOption = res.deptOption;
                  this.pattypeOption = res.pattypeOption;
                  this.rptunitOption = res.rptunitOption;
                  this.wardOption = res.wardOption;
                  this.labgroupOption = res.labgroupOption;
                  this.datetime1 = res.datetime;
                  this.datetime2 = res.datetime;
              });
      },
      footerMethod({ columns, data }) {
          return [
              columns.map((column, columnIndex) => {
                  var a = columns.indexOf(columns.find(x => x.property == "cp_cnt"));
                  if (columnIndex === 0) {
                      return "总计";
                  } else if (columnIndex >= a && column.property != "passrate") {
                      if (this.tableData.length > 0) return this.SumAll(this.tableData, column.property);
                      else return null;
                  } else if (column.property == "passrate") {
                      if (this.tableData.length > 0) return ((this.SumAll(this.tableData, "chkpass") / (this.SumAll(this.tableData, "chkpass") + this.SumAll(this.tableData, "chknopass"))) * 100).toFixed(2);
                  }
                  return null;
              })
          ];
      },
      footerCellClassName({ $rowIndex, column, columnIndex }) {},

      SumAll(data, property) {
          var plus = 0;
          if (data) {
              for (let i = 0, len = data.length; i < len; i++) {
                  plus += data[i][property] - 0;
              }
          }

          return plus;
      },
      popMultipleChk(e, fieldname, option, title) {
          let temp = XEUtils.clone(option, true);
          this.option1 = [];
          this.option2 = [];
          if (e != "") {
              let k = e.split(",");
              k.forEach(item => {
                  let ndata = temp.find(m => m.data == item);
                  if (ndata != undefined && ndata != null) {
                      this.option2.push(ndata);
                  }
              });
              temp.forEach(item => {
                  if (k.indexOf(item.data + "") < 0) {
                      this.option1.push(item);
                  }
              });
          } else {
              this.option1 = temp;
          }
          this.fieldname = fieldname;
          this.multichktitle = title;
          this.dialogMultipleVisible = true;
      },
      closedialog(value, name) {
          if (value != "close") {
              this[name] = value;
          }
          this.dialogMultipleVisible = false;
      },
      clearData(value, field) {
          this.tableData = [];
          if (value == "1") this.selectarr.push(field);
          else {
              var index = this.selectarr.indexOf(field);
              if (index > -1) this.selectarr.splice(index, 1);
          }
          //会导致表头渲染错位
      },
      //清除
      BtnClear() {
          this.datetime1 = "";
          this.datetime2 = "";
          this.rptunitid = "";
          this.reqdeptno = "";
          this.pattype = "";
          this.agenullflag = "0";
          this.deptnoflag = "0";
          this.hospitalflag = "0";
          this.pattypeflag = "0";
          this.sexflag = "0";
          this.diagflag = "0";
          this.diagicdflag = "0";
          this.testmethodflag = "0";
      },
      reportuserChange(code, name) {
          this.reportuser = code;
          this.reportusername = name;
      },
      labgroupChange(code, name) {
          this.labgroupid = code;
          this.labgroupname = name;
      },
      //导出
      BtnExport() {
          let tableColumn = this.$refs.xTable1.getColumns();
          let extableColumn = [];
          tableColumn.forEach(item => {
              extableColumn.push({ title: item.title, field: item.property });
          });
          let filtertxt = this.getExportFilter();
          this.$pub.exportTableExcel(this.tableData, extableColumn, "LIS634 报告自动审核统计.xlsx", filtertxt, "统计条件：");
      },
      // 获取当前导出文件的所有查询条件
      getExportFilter() {
          let filtertxt = "标本日期:" + XEUtils.toDateString(this.datetime1, "yyyy-MM-dd") + "至" + XEUtils.toDateString(this.datetime2, "yyyy-MM-dd") + "; ";
          if(this.reqdeptno) filtertxt += "申请科室:" + this.getOptionName(this.deptOption.filter((item) =>  ("," + this.reqdeptno + ",").indexOf("," + item.data + ",") > -1))+ "; ";
          let tatitemtxt = "";
          if(this.reportuserflag == "1") tatitemtxt+= "检验医生、";
          if(this.deptnoflag == "1") tatitemtxt+= "申请科室、";
          if(this.sexflag == "1") tatitemtxt+= "病人性别、";
          if(this.yearflag == "1") tatitemtxt+= "年、";
          if(this.chkuserflag == "1") tatitemtxt+= "审核医生、";
          if(this.wardflag == "1") tatitemtxt+= "病人病区、";
          if(this.pattypeflag == "1") tatitemtxt+= "病人类型、";
          if(this.monthflag == "1") tatitemtxt+= "月、";
          if(this.chktipsflag == "1") tatitemtxt+= "审核信息、";
          if(this.rptunitflag == "1") tatitemtxt+= "报告单元、";
          if(this.labgroupflag == "1") tatitemtxt+= "检验组别、";
          if(this.dayflag == "1") tatitemtxt+= "日、";
          if(tatitemtxt) filtertxt += "统计项目:" + tatitemtxt.substring(0, tatitemtxt.length - 1) + "; ";
          if(this.rptunitid) filtertxt += "报告单元:" + this.getOptionName(this.rptunitOption.filter((item) =>  ("," + this.rptunitid + ",").indexOf("," + item.data + ",") > -1))+ "; ";
          if(this.wardno) filtertxt += "病人病区:" + this.getOptionName(this.wardOption.filter((item) =>  ("," + this.wardno + ",").indexOf("," + item.data + ",") > -1))+ "; ";
          if(this.reportuser)  filtertxt += "检验医生:" + this.reportusername + "; ";
          if(this.labgroupid)  filtertxt += "检验组别:" + this.labgroupname + "; ";
          if(this.pattype) filtertxt += "病人类型:" + this.getOptionName(this.pattypeOption.filter((item) =>  ("," + this.pattype + ",").indexOf("," + item.data + ",") > -1))+ "; ";
          if(this.onlyautochk == "1")  "仅统计参与自动审核的报告; ";
          return filtertxt;
      },
      getOptionName(option) {
          if (option && option.length > 0) {
              let optionname = "";
              option.forEach((item) => {
                  if(item.hasOwnProperty("display")) {
                      optionname += item.display + ",";
                  } else {
                      optionname += item.label + ",";
                  }
              });
              return optionname.substring(0, optionname.length - 1);
          } else {
              return "";
          }
      },
      //打印
      BtnPrint(index) {
          let qwhere = "日期:" + XEUtils.toDateString(this.datetime1, "yyyy-MM-dd") + " - " + XEUtils.toDateString(this.datetime2, "yyyy-MM-dd");
          var qcol = "";
          if (this.selectarr.length > 0) {
              for (let i = 0; i < this.selectarr.length; i++) {
                  qcol += this.selectarr[i] + ",";
              }
              qcol = qcol.substring(0, qcol.length - 1);
          }

          let footerData = {};
          let columns = this.$refs.xTable1.getColumns();
          columns.forEach((item, index) => {
              if (index === 0) {
                  footerData[item.property] = "总计";
              }
              // let resdata = this.tableData.filter(x => {
              //     return x.color != "1" && x.color != "2";
              // });

              if (["chkpass", "cp_cnt", "chknopass", "elsenum"].includes(item.property)) {
                  footerData[item.property] = XEUtils.sum(this.tableData, item.property).toFixed(2);
              } else if (index > 0) {
                  footerData[item.property] = "";
              }
          });

          var datas = XEUtils.clone(this.tableData);
          datas.push(footerData);
          this.$pub.PrintDynamicCol(this, "6179", datas, this.$refs.xTable1.getTableColumn().tableColumn, qwhere, qcol, index);

          // var data = this.$refs.xTable1.afterFullData;
          // var userno = this.$tool.getSession(this.$tool.loginuserno);
          // if (data.length > 0) {
          //     const rload = this.$pub.openLoading(this, "正在打开报表设计器，请稍候……");
          //     let column = this.$refs.xTable1.getTableColumn().tableColumn;
          //     let str = JSON.stringify(this.$pub.InitColumn(column));
          //     str += "\r";
          //     str += JSON.stringify(data);
          //     let sql = btoa(encodeURIComponent(str));
          //     if (index == "1") {
          //         jsObj.PrintCustomJsonReport("6179", sql, userno, " 报告自动审核统计样式", "");
          //     } else {
          //         jsObj.SetCustomJsonPrintConfig("6179", sql, userno, " 报告自动审核统计样式");
          //     }
          //     rload.close();
          // } else {
          //     this.$alert("请选中要打印的数据！");
          // }
      }
  }
};
</script>

<style scoped>
.pagetop {
  width: 100%;
  height: 160px;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 165px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
table tr td:nth-child(1) {
  width: 60px;
}
.left_nav {
  width: 20%;
  height: 100%;
  border-right: 1px solid #ccc;
  padding: 0px 0px 0px 0px;
  float: left;
  display: inline;
}
.left_nav div {
  cursor: pointer;
  height: 40px;
  width: 100%;
  float: left;
  padding-left: 20px;
  line-height: 40px;
}
.left_nav .active {
  background-color: DeepSkyBlue;
}
.right_nav {
  width: 80%;
  height: 100%;
  display: inline-block;
  vertical-align: top;
  float: left;
  display: inline;
}
::v-deep .el-input--suffix .el-input__inner {
  padding-right: 10px;
}
</style>
